<?php

namespace App\Rpc\Client;

use App\Components\LoadBalance\RandLoadBalance;
use Swoft\Rpc\Client\Contract\ProviderInterface;

class Provider implements ProviderInterface
{

    protected $serviceName;

    public function __construct($serviceName)
    {
        $this->serviceName = $serviceName;
    }

    /**
     * 根据服务名称从consul中动态获取服务,负载均衡(加权重、轮询、随机)，缓存地址
     * @param \Swoft\Rpc\Client\Client $client
     * @return array
     */
    public function getList($client): array
    {
        $serverList = bean('consulProvider')->getServerList($this->serviceName);
        $serverList = RandLoadBalance::select($serverList);//随机加权重的方式获取服务
        print_r($serverList);
        //$serverList = array_column($serverList, 'address');
        //print_r($serverList);
        return [$serverList['address']];
    }

}
